﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using SoetemanSoftware.Umb.Datatypes.MacroPicker.Data;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.macro;
using umbraco.interfaces;
using umbraco.uicontrols;

namespace SoetemanSoftware.Umb.Datatypes.MacroPicker
{
    /// <summary>
    /// Settings editor to configure the Dataype
    /// </summary>
    public class MacroPickerPrevalueEditor : PlaceHolder , IDataPrevalue
    {
        #region Private fields
        private BaseDataType _datatype;
        private CheckBoxList _macroList = new CheckBoxList();
        private List<string> _allowedMacros;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataType"></param>
        public MacroPickerPrevalueEditor(BaseDataType dataType)
        {
            Datatype = dataType;
        }
        #endregion

        #region Overrides
        /// <summary>
        /// Initializes controls
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnLoad(e);
            PropertyPanel propertyPanel = new PropertyPanel();
            propertyPanel.Text = "Allowed Macros";
            propertyPanel.Controls.Add(MacroList);

            Controls.Add(propertyPanel);
        }

        /// <summary>
        /// Initialize the form
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            BuildForm();
        }

        #endregion

        #region EventHandlers
        private void MacroList_DataBound(object sender, EventArgs e)
        {
            CheckBoxList cbl = (CheckBoxList)sender;
            foreach (ListItem item in cbl.Items)
            {
                item.Selected = AllowedMacros.Contains(item.Value);
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Returns the selected Macro's in a comma seperated string
        /// </summary>
        private string GetSelectedMacosFromCheckList()
        {
            StringBuilder  result = new StringBuilder();
            foreach (ListItem lst in MacroList.Items)
            {
                if (lst.Selected)
                {
                    //User Selected the Macro, add to the string builder  
                    if (result.Length > 0)
                    {
                        //Allready item on the list add a comma first
                        result.Append(",");
                    }
                    result.Append(lst.Value);
                }
            }
            return result.ToString();
        }

        /// <summary>
        /// Create the form
        /// </summary>
        private void BuildForm()
        {
            if (!Page.IsPostBack)
            {
                MacroList.DataValueField = "Alias";
                MacroList.DataTextField = "Name";
                MacroList.DataSource = Macro.GetAll();
                MacroList.DataBound += new EventHandler(MacroList_DataBound);
            }

            MacroList.DataBind();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Holds a reference to all allowed Macros
        /// </summary>
        public List<string> AllowedMacros
        {
            get
            {
                if (_allowedMacros == null)
                {
                    _allowedMacros = MacroData.GetPreValues(Datatype.DataTypeDefinitionId);
                }
                return _allowedMacros;
            }
        }

        /// <summary>
        /// Holds a reference to the checkbox list containing all the macros
        /// </summary>
        private CheckBoxList MacroList
        {
            get { return _macroList; }
            set { _macroList = value; }
        }
        #endregion

        #region IDataPrevalue Members
        /// <summary>
        /// Reference to the datatype 
        /// </summary>
        public BaseDataType Datatype
        {
            get { return _datatype; }
            set { _datatype = value; }
        }

        /// <summary>
        /// Reference to the editor
        /// </summary>
        public Control Editor
        {
            get { return this; }
        }

        /// <summary>
        /// Save settings
        /// </summary>
        public void Save()
        {
            MacroData.SavePreValues(GetSelectedMacosFromCheckList(), Datatype.DataTypeDefinitionId);
        }
        #endregion
    }
}
